package com.cass.controller;

import com.cass.entity.StudentDetails;
import com.cass.entity.Vo.studentVo;
import com.cass.entity.student;
import com.cass.service.IBreakTheRuleService;
import com.cass.service.SocialPracticeService;
import com.cass.service.studentService;
import com.cass.uilts.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生类 控制器
 * @Author: wangyou
 *
 */
@Slf4j
@RestController
@RequestMapping("/student")
@RequiredArgsConstructor
@Tag(name = "studentController", description = "学生管理")
public class StudentController {

    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    private final SocialPracticeService SocialPracticeService;
    private final studentService studentService;
    private final IBreakTheRuleService BreakTheRuleService;
    @Operation(summary = "获取所有学生信息和违纪次数",description = "需要登录后访问")
    @ApiResponse(description = "返回获取所有学生信息和违纪次数", content = @Content(mediaType = "application/json", schema = @Schema(implementation = studentVo.class)))
    @GetMapping("/break")
    public Result<List<studentVo>> getStudentAllBreak(){
        logger.info("获取所有学生信息和违纪次数");
        List<studentVo> studentVo = studentService.listStudent();
        return Result.success(studentVo);

    }
    /**
     * 查询所有学生信息
     */
    @Operation(summary = "获取所有学生信息",description = "需要登录后访问")
    @ApiResponse(description = "返回获取所有学生信息", content = @Content(mediaType = "application/json", schema = @Schema(implementation = student.class)))
    @GetMapping()
    public Result<List<student>> getStudentAll(){
        logger.info("获取所有学生信息");
        return Result.success(studentService.list());

    }

    /**
     * 添加学生信息
     */
    @Operation(summary = "添加学生信息",description = "需要登录后访问")
    @ApiResponse(description = "添加成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = student.class)))
    @PostMapping()
    public Result<String> addStudent(@RequestBody student student){
        logger.info("添加学生信息{}",student);
        studentService.save(student);
        return Result.success("添加成功");
    }

    /**
     * 修改学生信息
     */
    @Operation(summary = "修改学生信息",description = "需要登录后访问")
    @ApiResponse(description = "修改成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = student.class)))
    @PutMapping
    public Result<String> updateStudent(@RequestBody student student){
        logger.info("修改学生信息");
        studentService.updateStudent(student);
        return Result.success("修改成功");
    }

    /**
     * 删除学生信息
     */
    @Operation(summary = "删除学生信息",description = "需要登录后访问")
    @ApiResponse(description = "删除成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = student.class)))
    @DeleteMapping("/{id}")
    public Result<String> deleteStudent(@PathVariable("id") Integer id){
        logger.info("删除学生信息");
        studentService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     *
     * 根据id查询学生信息
     */
    @Operation(summary = "根据id查询学生信息",description = "需要登录后访问")
    @ApiResponse(description = "返回根据id查询学生信息", content = @Content(mediaType = "application/json", schema = @Schema(implementation = student.class)))
    @GetMapping("/{id}")
    public Result<student> getStudentById(@PathVariable("id") Integer id){
        logger.info("根据id查询学生信息");
        return Result.success(studentService.getById(id));
    }
    /**
     * 批量删除学生信息
     */
    @Operation(summary = "批量删除学生信息",description = "需要登录后访问")
    @ApiResponse(description = "批量删除成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = student.class)))
    @DeleteMapping("/batchDelete")
    public Result<String> batchDelete(@RequestBody List<Integer> ids){
        logger.info("批量删除学生信息：{}", ids);
        studentService.removeByIds(ids);
        return Result.success("批量删除成功");
    }
    /**
     * 根据学号查询学生信息
     */
    @GetMapping("/studentNumber/{studentNumber}")
    public Result<?> getStudentByStudentNumber(@PathVariable("studentNumber") String studentNumber){
        logger.info("根据学号查询学生信息");
        StudentDetails studentDetails=new StudentDetails();
        // 根据学号查询学生id
        student studentById = studentService.getStudentById(studentNumber);
        if (studentById==null){
            return Result.error("没有该学生");
        }
        studentDetails.setId(studentById.getId());
        studentDetails.setName(studentById.getName());
        studentDetails.setStudentNumber(studentById.getStudentNumber());
        // 根据学生id查询学生活动描述
        List<String> activityDescriptions = SocialPracticeService.getActivityDescriptionsByStudentId(studentById.getId());
        studentDetails.setActivityDescription(activityDescriptions);
        // 根据学号查询违纪
        List<String> violationsByStudentNumber = BreakTheRuleService.getViolationsByStudentNumber(studentNumber);
        studentDetails.setViolationOfDiscipline(violationsByStudentNumber);




        return Result.success(studentDetails);
    }
}
